#!groovy

@Library('jenkinslibrary@master') _

//func from shareibrary
def build = new org.devops.build()
def deploy = new org.devops.deploy()
def tools = new org.devops.tools()
def gitlab = new org.devops.gitlab()
def toemail = new org.devops.toemail()
def sonar = new org.devops.sonarqube()
def sonarapi = new org.devops.sonarapi()
def nexus = new org.devops.nexus()
def artifactory = new org.devops.artifactory() 
def k8s = new org.devops.kubernetes()

def runOpts
//env
String buildType = "${env.buildType}"
String buildShell = "${env.buildShell}"
String deployHosts = "${env.deployHosts}"
String srcUrl = "${env.srcUrl}"
String branchName = "${env.branchName}"
String artifactUrl = "${env.artifactUrl}"


if ("${runOpts}" == "GitlabPush"){
    branchName = branch - "refs/heads/"
    
    currentBuild.description = "Trigger by ${userName} ${branch}"
    gitlab.ChangeCommitStatus(projectId,commitSha,"running")
    env.runOpts = "GitlabPush"

    
} else {
   userEmail = "2560350642@qq.com"
}


//pipeline
pipeline{
    agent { node { label "build"}}
    
    
    stages{

       stage("GetCode"){
            steps{
                script{
                    println("${branchName}")
                
                    tools.PrintMes("获取代码","green")
                    checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], 
                                      doGenerateSubmoduleConfigurations: false, 
                                      extensions: [], 
                                      submoduleCfg: [], 
                                      userRemoteConfigs: [[credentialsId: 'gitlab-admin-user', url: "${srcUrl}"]]])
                }
            }
        }
        stage("Build&Test"){
            steps{
                script{
                    tools.PrintMes("执行打包","green")
                    artifactory.main(buildType,buildShell)
                    artifactory.PushArtifact()
                    
            

                    //deploy.SaltDeploy("${deployHosts}","test.ping")
                    //deploy.AnsibleDeploy("${deployHosts}","-m ping ")
                }
            }
       }
       
       
        //并行
        stage('parallel01') {
       
         
          parallel {
            stage("QA"){
                steps {
                    script{
                        tools.PrintMes("搜索项目","green")
                        result = sonarapi.SerarchProject("${JOB_NAME}")
                        println(result)
                        
                        if (result == "false"){
                            println("${JOB_NAME}---项目不存在,准备创建项目---> ${JOB_NAME}！")
                            sonarapi.CreateProject("${JOB_NAME}")
                        } else {
                            println("${JOB_NAME}---项目已存在！")
                        }
                        
                        tools.PrintMes("配置项目质量规则","green")
                        qpName="${JOB_NAME}".split("-")[0]   //Sonar%20way
                        sonarapi.ConfigQualityProfiles("${JOB_NAME}","java",qpName)
                    
                        tools.PrintMes("配置质量阈","green")
                        sonarapi.ConfigQualityGates("${JOB_NAME}",qpName)
                    
                        tools.PrintMes("代码扫描","green")
                        sonar.SonarScan("test","${JOB_NAME}","${JOB_NAME}","src","${branchName}")
                        
    
                        sleep 30
                        tools.PrintMes("获取扫描结果","green")
                        result = sonarapi.GetProjectStatus("${JOB_NAME}")
                        
                        
                        println(result)
                        if (result.toString() == "ERROR"){
                            toemail.Email("代码质量阈错误！请及时修复！",userEmail)
                            error " 代码质量阈错误！请及时修复！"
                            
                            
                        } else {
                            println(result)
                        }
                    }
               }
           }
           
           //构建镜像
           stage("BuildImages"){
                steps{
                    script{
                        tools.PrintMes("构建上传镜像","green")
                        env.serviceName = "${JOB_NAME}".split("_")[0]
                       
                        withCredentials([usernamePassword(credentialsId: 'aliyun-registry-admin', passwordVariable: 'password', usernameVariable: 'username')]) {
                           
                           env.dockerImage = "registry.cn-beijing.aliyuncs.com/devopstest/${serviceName}:${branchName}"
                           sh """
                               docker login -u ${username} -p ${password}  registry.cn-beijing.aliyuncs.com
                               docker build -t registry.cn-beijing.aliyuncs.com/devopstest/${serviceName}:${branchName} .
                               sleep 1
                               docker push registry.cn-beijing.aliyuncs.com/devopstest/${serviceName}:${branchName}
                               sleep 1
                               #docker rmi registry.cn-beijing.aliyuncs.com/devopstest/${serviceName}:${branchName}
                            """
                        }
                    }
                }
            }
        }
    }
       
       //发布
       stage("Deploy"){
            steps{
                script{
                    tools.PrintMes("发布应用","green")
                    
                    //下载版本库文件 
                    
                    releaseVersion = "${branchName}".split("-")[-1]
                    response = gitlab.GetRepoFile(7,"demo-uat%2f${releaseVersion}-uat.yaml")
                    //println(response)
                    
                    //替换文件中内容（镜像）
                    fileData = readYaml text: """${response}"""
                    println(fileData["spec"]["template"]["spec"]["containers"][0]["image"])
                    println(fileData["metadata"]["resourceVersion"])
                    oldImage = fileData["spec"]["template"]["spec"]["containers"][0]["image"] 
                    oldVersion = fileData["metadata"]["resourceVersion"]
                    oldUid = fileData["metadata"]["uid"]
                    response = response.replace(oldImage,dockerImage)
                    response = response.replace(oldVersion,"")
                    response = response.replace(oldUid,"")
                   
                    println(response)
                    
                    //更新gitlab文件内容
                    base64Content = response.bytes.encodeBase64().toString()
                    gitlab.UpdateRepoFile(7,"demo-uat%2f${releaseVersion}-uat.yaml",base64Content)
                    
                    //发布kubernetes
                    k8s.UpdateDeployment("demo-uat","demoapp",response)
                    

                }
            }
        }
       
        //接口自动化测试
        stage("InterfaceTest"){
            steps{
                script{
                    tools.PrintMes("接口测试","green")

                }
            }
       }
       
       
       
       
    }
    post {
        always{
            script{
                println("always")
            }
        }
        
        success{
            script{
                println("success")
                if ("${runOpts}" == "GitlabPush"){
                    gitlab.ChangeCommitStatus(projectId,commitSha,"success")
                }
                toemail.Email("流水线成功",userEmail)
            
            }
        
        }
        failure{
            script{
                println("failure")
                if ("${runOpts}" == "GitlabPush"){
                    gitlab.ChangeCommitStatus(projectId,commitSha,"failed")
                }
                toemail.Email("流水线失败了！",userEmail)
            }
        }
        
        aborted{
            script{
                println("aborted")
                if ("${runOpts}" == "GitlabPush"){
                    gitlab.ChangeCommitStatus(projectId,commitSha,"canceled")
                }
               toemail.Email("流水线被取消了！",userEmail)
            }
        
        }
    
    }
    
    
}
